package com.googlecode.i78ba.test.rss;

import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.util.Random;

import javax.xml.transform.stream.StreamSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Unmarshaller;

import com.googlecode.i78ba.forum.Post;
import com.googlecode.i78ba.forum.PostMapper;
import com.googlecode.i78ba.forum.Reply;
import com.googlecode.i78ba.forum.ReplyMapper;

public class RSSMain {

	private static ApplicationContext appContext;
	
	private static Random r = new Random(System.currentTimeMillis());
	
	/**
	 * http://www.163.com/rss
	 */
	public static void main(String[] args){
		appContext = new ClassPathXmlApplicationContext(
				"spring-application.xml");
		System.out.println("-------------------------------------------------------------------------------------");
		
		String[] urls=new String[]{
				"http://news.163.com/special/00011K6L/rss_newstop.xml",
				"http://news.163.com/special/00011K6L/rss_gn.xml",
				"http://news.163.com/special/00011K6L/rss_gj.xml",
				"http://news.163.com/special/00011K6L/rss_sh.xml",
				"http://news.163.com/special/00011K6L/rss_war.xml",
				"http://news.163.com/special/00011K6L/rss_hotnews.xml",
				"http://news.163.com/special/00011K6L/rss_newsspecial.xml",
				"http://news.163.com/special/00011K6L/rss_photo.xml",
				"http://news.163.com/special/00011K6L/rss_discovery.xml"
		};
		
		RSSMain m = new RSSMain();
		
		for (String urlStr : urls) {
			new Thread(m.new RunOneSite(urlStr)).start();
			try {
				Thread.sleep(1000L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	class RunOneSite implements Runnable{

		private String urlStr;
		
		RunOneSite(String urlStr){
			this.urlStr=urlStr;
		}
		
		@Override
		public void run() {
			try {
				Unmarshaller unmarshaller = (Unmarshaller)appContext.getBean(Unmarshaller.class);
				System.out.println("unmarshaller -> " + unmarshaller);
				URL url = new URL(urlStr);
				URLConnection uc = url.openConnection(); 
				RSS rss = (RSS)unmarshaller.unmarshal(new StreamSource(uc.getInputStream()));
				System.out.println("RSS -> " + rss.getChannel().getTitle());
				System.out.println("RSS -> " + rss.getChannel().getLink());
				System.out.println("RSS -> " + rss.getChannel().getDescription());
				System.out.println("RSS -> " + rss.getChannel().getPubDate());
				System.out.println("RSS -> " + rss.getChannel().getLastBuildDate());
				
				PostMapper postMapper = (PostMapper)appContext.getBean(PostMapper.class);
				ReplyMapper replyMapper = (ReplyMapper)appContext.getBean(ReplyMapper.class);
				for (Item item : rss.getChannel().getItems()) {
					System.out.println("ITEM -> " + item.getTitle());
					System.out.println("ITEM -> " + item.getLink());
					System.out.println("ITEM -> " + item.getDescription());
					System.out.println("ITEM -> " + item.getPubDate());
					
					Post post = new Post();
					post.setTitle(item.getTitle());
					post.setTopic_id(1);
					post.setCreate_time(new Timestamp(System.currentTimeMillis()));
					post.setLast_update_time(new Timestamp(System.currentTimeMillis()));
					post.setReply_num(1);
					postMapper.insert(post);
					Integer post_id = postMapper.lastInsertId();
					
					Reply reply = new Reply();
					reply.setPost_id(post_id);
					reply.setContent(item.getDescription()+item.getLink());
					reply.setTime(new Timestamp(System.currentTimeMillis()));
					replyMapper.insert(reply);
					
					int num = r.nextInt(10);
					for (int j = 0; j < num; j++) {
						Reply reply2 = new Reply();
						reply2.setPost_id(post_id);
						reply2.setContent("up " + num);
						reply2.setTime(new Timestamp(System.currentTimeMillis()));
						replyMapper.insert(reply2);
						postMapper.addReplyNumById(post_id);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
